package com.community.process._1010.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.community.process._1010.common.api.CommonResult;
import com.community.process._1010.common.api.ErrorAssert;
import com.community.process._1010.common.utils.ExternalApiUtils;
import com.community.process._1010.common.utils.PageUtil;
import com.community.process._1010.common.utils.mp.MpPageFormat;
import com.community.process._1010.dao.entity.ProcessingCouponType;
import com.community.process._1010.dao.mapper.ProcessingCouponTypeMapper;
import com.community.process._1010.dao.vo.ProcessingCouponTypeVo;
import com.community.process._1010.domains.ProcessingCouponTypeDTO;
import com.community.process._1010.domains.external.ProcessingDato;
import com.community.process._1010.service.ProcessingCouponTypeService;
import com.oneadds.lowcode.BaseServiceImpl;
import com.oneadds.lowcode.dto.PageListData;
import org.springframework.stereotype.Service;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

@Service
public class ProcessingCouponTypeServiceImpl extends BaseServiceImpl<ProcessingCouponTypeMapper, ProcessingCouponType> implements ProcessingCouponTypeService {

    @Override
    public PageListData<ProcessingCouponTypeVo> getProcessingCouponTypeList(ProcessingCouponTypeDTO.ProcessingCouponTypeSearch dto) {
        QueryWrapper<ProcessingCouponTypeVo> wrapper = new QueryWrapper<>();
        //数据分页
        Page<ProcessingCouponTypeVo> page = this.baseMapper.getProcessingCouponTypeVo(PageUtil.page(dto), wrapper);
        List<ProcessingCouponTypeVo> records = page.getRecords();
        // 调用外部API获取数据
        CommonResult<List<ProcessingDato>> processingDataResult = ExternalApiUtils.getCouponProcessing(dto);
        // 判断接口是否成功
        if (processingDataResult == null || processingDataResult.getCode() != 200) {
            // 如果外部API调用失败，可以抛出异常或返回默认值
            ErrorAssert.notNull(processingDataResult, "接口调用失败");
            Serializable serializable = processingDataResult != null ? processingDataResult.getCode() : "null";
            return MpPageFormat.format(page);
        }

        List<ProcessingDato.Record> processingDataList = processingDataResult.getData().get(0).getRecords();
        // 获取page中所有的id字段
        for (ProcessingCouponTypeVo record : records) {
            // 用一个列表存储所有匹配的amount字段
            List<Integer> matchedAmounts = new ArrayList<>();
            // 遍历processingDataList进行匹配
            for (ProcessingDato.Record processingData : processingDataList) {
                // 如果ID匹配
                if (record.getId().equals(processingData.getProcessingCouponTypeId())) {
                    matchedAmounts.add(Math.toIntExact(processingData.getAmount()));
                }
            }

            // 如果匹配到多个amount值，选择最小的
            if (!matchedAmounts.isEmpty()) {
                // 找到最小的amount值
                Integer minAmount = Collections.min(matchedAmounts);
                // 将最小的amount值赋值给page中的amount字段
                record.setAmount(Long.valueOf(minAmount / 10000L));
            } else {
                // 如果没有匹配到，赋默认值10
                record.setAmount(10L);
            }
        }
        return MpPageFormat.format(page);
    }

    @Override
    public PageListData<ProcessingCouponTypeVo> getProcessingMioiCouponTypeList(ProcessingCouponTypeDTO.ProcessingCouponTypeSearch dto) {
        QueryWrapper<ProcessingCouponTypeVo> wrapper = new QueryWrapper<>();
        //数据分页
        Page<ProcessingCouponTypeVo> page = this.baseMapper.getProcessingCouponTypeVo(PageUtil.page(dto), wrapper);
        List<ProcessingCouponTypeVo> records = page.getRecords();
        // 调用外部API获取数据
        CommonResult<List<ProcessingDato>> processingDataResult = ExternalApiUtils.getCouponMioiProcessing(dto);
        // 判断接口是否成功
        // if (processingDataResult == null || processingDataResult.getCode() != 200) {
        //     // 如果外部API调用失败，可以抛出异常或返回默认值
        //     ErrorAssert.notNull(processingDataResult, "接口调用失败");
        //     Serializable serializable = processingDataResult != null ? processingDataResult.getCode() : "null";
        //     return MpPageFormat.format(page);
        // }

        List<ProcessingDato.Record> processingDataList = processingDataResult.getData().get(0).getRecords();
        // 获取page中所有的id字段
        for (ProcessingCouponTypeVo record : records) {
            // 用一个列表存储所有匹配的amount字段
            List<Integer> matchedAmounts = new ArrayList<>();
            // 遍历processingDataList进行匹配
            for (ProcessingDato.Record processingData : processingDataList) {
                // 如果ID匹配
                if (record.getId().equals(processingData.getProcessingCouponTypeId())) {
                    matchedAmounts.add(Math.toIntExact(processingData.getAmount()));
                }
            }

            // 如果匹配到多个amount值，选择最小的
            if (!matchedAmounts.isEmpty()) {
                // 找到最小的amount值
                Integer minAmount = Collections.min(matchedAmounts);
                // 将最小的amount值赋值给page中的amount字段
                record.setAmount(Long.valueOf(minAmount / 10000L));
            } else {
                // 如果没有匹配到，赋默认值10
                record.setAmount(10L);
            }
        }
        return MpPageFormat.format(page);
    }
}
